/* 
 * Copyright (c) 2019, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.config;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.cory.admin.sys.domain.AdminPrincipal;
import com.cory.admin.sys.service.SysLoginLogService;
import com.cory.boot.component.cache.RedisUtils;
import com.cory.boot.utils.SpringUtils;

/**
 * session监听
 * 用了spring session会导致该监听器失效
 * @author xiongcong
 * @date 2019-02-27
 */
@WebListener
public class SessionListener implements HttpSessionListener, HttpSessionAttributeListener {

    @Override
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        
        AdminPrincipal user = (AdminPrincipal)session.getAttribute(AdminConstants.SESSION_USER_INFO);
        if (user != null) {
            RedisUtils.del(AdminConstants.REDIS_PREFIX_TOKEN + user.getToken());
            if (user.getLoginId() != null) {
                SysLoginLogService sysLoginLogService = SpringUtils.getBean(SysLoginLogService.class);
                sysLoginLogService.addLoginoutLog(user.getLoginId(), "会话信息失效退出");
                AdminConstants.MAP_LOGIN_TIP_MSG.put(session.getId(), "会话已失效，请重新登录");
            }
        }
    }
}
